package com.wiim.processor;

import java.util.Collections;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import com.wiim.connector.RestClient;
import com.wiim.model.UserData;

import android.util.Log;

public class CMProcessor extends BaseModuleProcessor {

	private final static String URL_LOGIN_VALIDATE = "user/login/validate/?format=json";
	private final static String URL_LOGIN_REGISTER = "user/registernew/?format=json";

	public UserData LoginValidate(UserData uData){
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
		HttpEntity<UserData> requestEntity = new HttpEntity<UserData>(uData, requestHeaders);
		RestClient client = new RestClient();
		String url = String.format(URL_LOGIN_VALIDATE);	
				
		UserData res = new UserData();
		try{					
			ResponseEntity<UserData> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.PUT, requestEntity,UserData.class);
            res = response.getBody();
		} catch (Exception ex) {
				Log.i("loginvalidate", ex.getMessage());
		}			
		return res;
	}	
	
	public UserData MobileUserRegister(UserData uData){
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
		HttpEntity<UserData> requestEntity = new HttpEntity<UserData>(uData, requestHeaders);
		RestClient client = new RestClient();
		String url = String.format(URL_LOGIN_REGISTER);	
				
		UserData res = new UserData();
		try{					
			ResponseEntity<UserData> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.PUT, requestEntity,UserData.class);
            res = response.getBody();
		} catch (Exception ex) {
				Log.i("MobileUserRegister", ex.getMessage());
		}			
		return res;
	}	
	
	private final static String URL_TEST = "call/test?format=json";
	public String getHelloWorld() {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		String res ="" ;
		try {
			RestClient client = new RestClient();
			String url = String.format(URL_TEST);

			ResponseEntity<String> response = client.template().exchange(
					RestClient.apiUrl(url), HttpMethod.GET, requestEntity,
					String.class);
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("getHelloWorld", ex.getMessage());
		}		
		return res;
	}
	


}
